<template>
  <div class="reset-password-container">
    <h2>重置密码</h2>
    <el-form :model="formData" :rules="rules" ref="resetPasswordForm" label-width="100px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input v-model="formData.oldPassword" type="password" placeholder="请输入旧密码" show-password></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="formData.newPassword" type="password" placeholder="请输入新密码" show-password></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="confirmPassword">
        <el-input v-model="formData.confirmPassword" type="password" placeholder="请再次输入新密码" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitResetPassword">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>


//用户头像地址
 import {userPasswordUpdateService} from '@/api/user.js'
import {ElMessage} from 'element-plus'
//头像修改
const imgUrl = async ()=>{
    //调用接口
    let result = await userPasswordUpdateService(imgUrl.value);
    if(result.code === 200){
    }
}

export default {
  name: 'UserResetPassword',
  data() {
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.formData.newPassword) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    };
    return {
      formData: {
        username: '',
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        oldPassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少为6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入新密码', trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
     submitResetPassword : () => {
      
  resetPasswordFormRef.value.validate((valid) => {
    if (valid) {
      const formData = new FormData();
      formData.append('oldPassword', formData.value.oldPassword);
      formData.append('newPassword', formData.value.newPassword);

      fetch('/api/user.js', {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${tokenStore.token}` // 假设您使用Bearer Token进行身份验证
        },
        body: formData
      })
      .then(response => response.json())
      .then(data => {
        ElMessage.success('密码重置成功');
        resetForm();
      })
      .catch(error => {
        ElMessage.error(error.msg || '密码重置失败');
      });
    } else {
      ElMessage.error('表单验证失败');
    }
  });
},
    resetForm() {
      this.$refs.resetPasswordForm.resetFields();
    }
  }
};
</script>

<style scoped>
.reset-password-container {
  width: 400px;
  margin: 50px auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
}
</style>